package cn.murky.admin.quartz.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.murky.admin.quartz.domain.dto.SysJobLogPageDTO;
import cn.murky.admin.quartz.domain.entity.SysJobLog;
import cn.murky.admin.quartz.domain.entity.table.SysJobLogTableDef;
import cn.murky.admin.quartz.service.ISysJobLogService;
import cn.murky.core.web.ApiResult;
import cn.murky.core.web.BaseController;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.validation.annotation.Valid;

import java.util.Arrays;

/**
 * 调度日志操作处理
 *
 * @auth hans
 */
@Controller
@Valid
@Mapping("monitor/jobLog")
public class SysJobLogController extends BaseController<ISysJobLogService> {

    /**
     * 查询定时任务调度日志列表
     */
    @SaCheckPermission("jobLog")
    @Mapping("/list")
    @Get
    public ApiResult<Page<SysJobLog>> list(SysJobLogPageDTO sysJobLogPageDTO) {
        SysJobLogTableDef SYS_JOB_LOG = SysJobLogTableDef.SYS_JOB_LOG;
        QueryWrapper queryWrapper = QueryWrapper.create().where(SYS_JOB_LOG.JOB_NAME.like(sysJobLogPageDTO.getJobName())
                .and(SYS_JOB_LOG.JOB_GROUP.eq(sysJobLogPageDTO.getJobGroup()))
                .and(SYS_JOB_LOG.STATUS.eq(sysJobLogPageDTO.getStatus()))
                .and(SYS_JOB_LOG.INVOKE_TARGET.like(sysJobLogPageDTO.getInvokeTarget()))
                .and(SYS_JOB_LOG.CREATE_TIME.between(sysJobLogPageDTO.getOffsetDateTimes()[0], sysJobLogPageDTO.getOffsetDateTimes()[1])));
        Page<SysJobLog> page = baseService.page(sysJobLogPageDTO, queryWrapper);
        return ApiResult.ok(page);
    }


    /**
     * 根据调度编号获取详细信息
     */
    @SaCheckPermission("jobLog")
    @Mapping(value = "/{jobLogId}")
    @Get
    public ApiResult<SysJobLog> getInfo(Long jobLogId) {
        return ApiResult.ok(baseService.getById(jobLogId));
    }


    /**
     * 删除定时任务调度日志
     */
    @SaCheckPermission("job:remove")
    @Mapping("/{jobLogIds}")
    @Get
    public ApiResult<?> remove(Long[] jobLogIds) {
        return ApiResult.ok(baseService.removeByIds(Arrays.stream(jobLogIds).toList()));
    }

    /**
     * 清空定时任务调度日志
     */
    @SaCheckPermission("job:remove")
    @Mapping("/clean")
    @Get
    public ApiResult<?> clean() {
        return toResult(baseService.remove(QueryWrapper.create()));
    }
}
